<template>
  <div id="home">
    <NabBar class="home-nav">
      <div slot="center">购物街</div>
    </NabBar>
    <scroll
      :pull-up-load="true"
      @scroll="contentScroll"
      class="content"
      ref="scroll"
      :probeType="3"
      @pullingUp="loadMore"
    >
      <HomeSwiper :banners="banner" />
      <HomeRecommendView :recommends="recommend" />
      <FeatureView />
      <TabControl class="tab-control" :titles="['流行','新款','精选']" @tabClick="tabClick" />
      <goods-list :goods="showGoods" />
    </scroll>
    <backtop @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>
<script>
import HomeSwiper from './childComps/HomeSwiper.vue'
import HomeRecommendView from './childComps/HomeRecommendView.vue'
import FeatureView from './childComps/HomeFeature.vue'

import NabBar from 'components/common/navbar/navbar'
import TabControl from 'components/content/tabControl/TabControl'
import Scroll from 'components/common/scroll/scroll.vue'
import GoodList from 'components/content/goods/GoodsListItem'
import Backtop from '../../components/content/backtop/backtop.vue'

import { getHomeMultidata, getHomeGoods } from 'network/home'
import GoodsList from '../../components/content/goods/GoodsList.vue'
export default {
  data() {
    return {
      banner: [],
      recommend: [],
      goods: {
        'pop': { page: 0, list: [] },
        'new': { page: 0, list: [] },
        'sell': { page: 0, list: [] }
      },
      currentType: 'pop',
      isShowBackTop: false
    }
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  components: {
    HomeSwiper,
    HomeRecommendView,
    FeatureView,
    GoodsList,
    Backtop,
    Scroll,
    NabBar,
    TabControl,
    GoodList
  },
  created() {
    // 1.请求多个数据
    this.getHomeMultidata()
    // 2.请求商品数据
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')
  },
  methods: {
    /**
     * 事件监听相关的方法
     */
    loadMore() {
      this.getHomeGoods(this.currentType)
    },
    contentScroll(position) {
      this.isShowBackTop = position.y < -1000
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0)
    },
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break;
        case 1:
          this.currentType = 'new'
          break;
        case 2:
          this.currentType = 'sell'
          break
      }
    },
    /**
     * 网络请求相关的方法
     */
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        this.banner = res.data.banner.list
        this.recommend = res.data.recommend.list
      })
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page += 1

        this.$refs.scroll.finishPullUp()
      })
    }
  }
}
</script>
<style scoped>
#home {
  padding-top: 44px;
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1;
}
.tab-control {
  position: sticky;
  top: 44px;
  background-color: #fff;
  z-index: 9;
}
.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>